package com.heima.user.service.impl;

import java.util.Date;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.common.exception.CustomException;
import com.heima.feigns.article.ArticleFeign;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.dtos.UserRelationDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFan;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.mapper.ApUserFanMapper;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author zhanghao
 * @date 2021/4/20 20:10
 */
@Service
@Transactional
public class ApUserRelationServiceImpl implements ApUserRelationService {
    @Autowired
    ArticleFeign articleFeign;
    @Resource
    ApUserMapper apUserMapper;

    @Override
    public ResponseResult follow(UserRelationDto dto) {
        //检查参数
        if (dto.getAuthorId() == null) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID, "作者ID不存在");
        }
        if (dto.getOperation().intValue() != 0 && dto.getOperation().intValue() != 1) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID, "关注的类型错误");
        }
        //检查是否登录
        ApUser loginUser = AppThreadLocalUtils.getUser();
        if (loginUser == null) {
            throw new CustomException(AppHttpCodeEnum.LOGIN_USER_STATUS_INVALID, "请先登录，再关注");
        }
        ApAuthor apAuthor = articleFeign.selectById(dto.getAuthorId());
        if (apAuthor == null) {
            throw new CustomException(AppHttpCodeEnum.DATA_NOT_EXIST, "作者信息不存在");
        }
        if (loginUser.getId().equals(apAuthor.getUserId())) {
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID, "自己不能关注自己");
        }
        if (dto.getOperation().intValue() == 0) {
            followByUserId(loginUser.getId(), apAuthor);
        } else {
            followCancelByUserId(loginUser.getId(), apAuthor.getUserId());
        }
        return ResponseResult.okResult();
    }

    private void followCancelByUserId(Integer loginUserId, Integer authorUserId) {
        // 删除 关注信息表中相关数据
        apUserFollowMapper.delete(Wrappers.<ApUserFollow>lambdaQuery()
                .eq(ApUserFollow::getUserId, loginUserId)
                .eq(ApUserFollow::getFollowId, authorUserId));
        // 删除 粉丝信息表中相关数据
        apUserFanMapper.delete(Wrappers.<ApUserFan>lambdaQuery()
                .eq(ApUserFan::getUserId, authorUserId)
                .eq(ApUserFan::getFansId, loginUserId));
    }

    @Resource
    ApUserFanMapper apUserFanMapper;
    @Resource
    ApUserFollowMapper apUserFollowMapper;

    private void followByUserId(Integer loginUserId, ApAuthor author) {
        //查看是否已经关注过
        ApUserFollow apUserFollow = apUserFollowMapper.selectOne(Wrappers.<ApUserFollow>lambdaQuery()
                .eq(ApUserFollow::getUserId, loginUserId).eq(ApUserFollow::getFollowId, author.getUserId()));
        if (apUserFollow != null) {
            throw new CustomException(AppHttpCodeEnum.DATA_EXIST, "你已经关注过了");
        }
        apUserFollow = new ApUserFollow();
        apUserFollow.setUserId(loginUserId);
        apUserFollow.setFollowId(author.getUserId());
        apUserFollow.setFollowName(author.getName());
        apUserFollow.setLevel((short) 0);
        apUserFollow.setIsNotice(false);
        apUserFollow.setCreatedTime(new Date());
        apUserFollowMapper.insert(apUserFollow);

        //查看是否已经是粉丝
        ApUserFan apUserFan = apUserFanMapper.selectOne(Wrappers.<ApUserFan>lambdaQuery()
                .eq(ApUserFan::getUserId, author.getUserId()).eq(ApUserFan::getFansId, loginUserId));
        if (apUserFan != null) {
            throw new CustomException(AppHttpCodeEnum.DATA_EXIST, "你已经成为该作者的粉丝");
        }
        ApUser apUser = apUserMapper.selectById(loginUserId);
        apUserFan = new ApUserFan();
        apUserFan.setUserId(author.getUserId());
        apUserFan.setFansId(Long.valueOf(loginUserId));
        apUserFan.setFansName(apUser.getName());
        apUserFan.setLevel((short) 0);
        apUserFan.setCreatedTime(new Date());
        apUserFan.setIsDisplay(false);
        apUserFan.setIsShieldLetter(false);
        apUserFan.setIsShieldComment(false);
        apUserFanMapper.insert(apUserFan);
    }
}
